using Digitalmes.Application.Mes.Processes.StepMaterials;
using Digitalmes.Application.Mes.Processes.StepMaterials.Commands;
using Digitalmes.Application.Mes.Processes.StepMaterials.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Processes;

/// <summary>
/// 工序物料终结点。
/// </summary>
public sealed class StepMaterialEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/process", "stepMaterial")
            .RequireAuthorization()
            .MapGet(GetStepMaterialListAsync, "list")
            .MapPost(CreateStepMaterialAsync)
            .MapPut(UpdateStepMaterialAsync, "{id}")
            .MapDelete(DeleteStepMaterialAsync, "{id}");
    }

    [EndpointDescription("查询工艺物料集合")]
    public async Task<IApiResult> GetStepMaterialListAsync(ISender sender, [AsParameters] GetStepMaterialListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询工艺物料")]
    public async Task<IApiResult> CreateStepMaterialAsync(ISender sender, ProcStepMaterialDto dto)
    {
        var result = await sender.Send(new CreateStepMaterialCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新工艺物料")]
    public async Task<IApiResult> UpdateStepMaterialAsync(ISender sender, long id, ProcStepMaterialDto dto)
    {
        var result = await sender.Send(new UpdateStepMaterialCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除工艺物料")]
    public async Task<IApiResult> DeleteStepMaterialAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteStepMaterialCommand(id));
        return result.ToApiResult();
    }
}
